﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;
using Sunny;
using Sunny.UI.Win32;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace ALL_Tool
{
    public partial class FCustomMain : Sunny.UI.UIForm
    {
        public FCustomMain()
        {
            InitializeComponent();
            Ip.Text = "127.0.0.1";
            Port.Text = "1433"; // 默认 SQL Server 端口
            Database.Text = "hbposv10";
            Username.Text = "sa";
            Password.Text = "";
        }
        //logo
        private void Form1_Load(object sender, EventArgs e)
        {
           //
            
        }
        private void tabPage1_Click(object sender, EventArgs e)
        {

        }
        //页面跳转  123
        private void uiButton1_Click(object sender, System.EventArgs e)
        {
            if (!ExistPage(1001))
            {
                AddPage(new EncryptionExport());
            }
            SelectPage(1001);
        }
        private void uiButton2_Click(object sender, System.EventArgs e)
        {
            if (!ExistPage(1002))
            {
                AddPage(new DataExport());
            }

            SelectPage(1002);
        }
        private void uiButton3_Click(object sender, System.EventArgs e)
        {
            if (!ExistPage(1003))
            {
                AddPage(new FPage3());
            }

            SelectPage(1003);
        }

        private void uiScrollingText1_Click(object sender, EventArgs e)
        {

        }

        private void uiLedLabel1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }


        //登录程序并更新配置文件，没有则生成保存
        private void btnLogin_Click(object sender, EventArgs e)
        {
            // 从输入框获取数据
            string ip = Ip.Text;
            string port = Port.Text;
            string database = Database.Text;
            string username = Username.Text;
            string password = Password.Text;
            // 假设你的ALL_Tool.exe.config文件路径是当前目录下的"ALL_Tool.exe.config"
            string appConfigPath = "ALL_Tool.exe.config";
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var appSettings = config.AppSettings.Settings;


            ///如果配置文件中不存在 appSettings  则创建 并赋予对应的默认值
            if (appSettings["ip"] == null || string.IsNullOrEmpty(appSettings["ip"].Value))
            {
                // 更新或添加新的配置项
                appSettings.Remove("ip"); // 移除可能存在的旧值
                appSettings.Add("ip", "127.0.0.1");
                appSettings.Add("port", "1433");
                appSettings.Add("database", "hbposv10");
                appSettings.Add("username", "sa");
                appSettings.Add("password", "");
                // 保存修改
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
            }      
            // 读取现有配置文件
            XDocument xdoc = XDocument.Load(appConfigPath);
            var settings = xdoc.Descendants("add")
                .Where(s => s.Attribute("key") != null)
                .ToDictionary(s => s.Attribute("key").Value, s => s.Attribute("value").Value);
            // 更新配置值
            settings["ip"] = ip;
            settings["port"] = port;
            settings["database"] = database;
            settings["username"] = username;
            settings["password"] = password;

            // 重新写入配置文件
            foreach (var setting in settings)
            {
                var element = xdoc.Descendants("add")
                    .FirstOrDefault(s => s.Attribute("key").Value == setting.Key);
                if (element != null)
                {
                    element.SetAttributeValue("value", setting.Value);
                }
                // 保存配置文件
                config.Save();
                // 刷新配置管理器
                ConfigurationManager.RefreshSection("appSettings");
            }
            xdoc.Save(appConfigPath);
            // 创建连接字符串
            string connectionString = $"Server={ip},{port};Database={database};User Id={username};Password={password};";

            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    // 打开连接
                    connection.Open();
                    MessageBox.Show("登录成功");

                    //连接成功后跳转到第二界面
                    if (!ExistPage(1002))
                    {
                        AddPage(new DataExport());
                    }

                    SelectPage(1002);
                }
            }
            catch (Exception ex)
            {
                // 显示错误信息
                MessageBox.Show("连接失败: " + ex.Message);
            }
        }

        private void Ip_TextChanged(object sender, EventArgs e)
        {

        }

        private void Port_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
